package com.bh.weapp.controller.sysuser.ledger;

import com.bh.common.annotation.Log;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.enums.BusinessType;
import com.bh.common.utils.StringUtils;
import com.bh.fee.domain.JcFeeType;
import com.bh.fee.service.IJcFeeTypeService;
import com.bh.ledger.convert.JcLedgerDetailConvert;
import com.bh.ledger.domain.JcLedgerDetail;
import com.bh.ledger.service.IJcLedgerDetailService;
import com.bh.ledger.vo.JcLedgerDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * 费用台帐明细Controller
 *
 * @author slx
 * @date 2023-04-24
 */
@Api(tags = {"SYS费用台帐明细Api"})
@RestController
@RequestMapping("/api/sysuser/ledger")
public class JcLedgerDetailSysApiController extends BaseController {

    @Autowired
    private IJcLedgerDetailService jcLedgerDetailService;

    @Autowired
    private IJcFeeTypeService jcFeeTypeService;

    @Autowired
    private JcLedgerDetailConvert jcLedgerDetailConvert;

    /**
     * 查询费用类型列表
     *
     * @return
     */
    @ApiOperation("查询费用类型列表")
    @GetMapping("/feeTypeList")
    public AjaxResult jcFeeTypeList() {
        JcFeeType jcFeeType = new JcFeeType();
        jcFeeType.setCode("SHSLF");
        List<JcFeeType> feeTypes = jcFeeTypeService.selectJcFeeTypeList(jcFeeType);
        return AjaxResult.success(feeTypes);
    }

    /**
     * 新增保存费用台帐明细，只能新增几种特殊类型
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("新增保存费用")
    @PreAuthorize("@ss.hasPermi('api:ledger:add')")
    @Log(title = "费用台帐明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody JcLedgerDetailVo jcLedgerDetailVo) {
        if (StringUtils.isEmpty(jcLedgerDetailVo.getJcOrderId())) {
            return error("请选择订单添加");
        }
        JcLedgerDetail ledgerDetail = jcLedgerDetailConvert.toDO(jcLedgerDetailVo);
        if (ledgerDetail != null) {
            ledgerDetail.setTaxrate(BigDecimal.valueOf(0.09));
            ledgerDetail.setIsConfirm(0);
            ledgerDetail.setCollectMethod("0");
            ledgerDetail.setSettlementMethod("0");
            //更具选中的类型判断
            ledgerDetail.setInput(jcLedgerDetailVo.getAmount().multiply(BigDecimal.valueOf(0.09)));
            ledgerDetail.setIncome(ledgerDetail.getAmount().subtract(ledgerDetail.getInput()));

            // 默认只能新增部分类型
            JcFeeType feeType = jcFeeTypeService.selectJcFeeTypeById(ledgerDetail.getJcFeeTypeId());
            return toAjax(jcLedgerDetailService.insertJcLedgerDetail(ledgerDetail));
        }
        return error("请添加正确数据");
    }

    /**
     * 修改保存费用台帐明细
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("修改保存费用台帐明细")
    @PreAuthorize("@ss.hasPermi('api:ledger:edit')")
    @Log(title = "费用台帐明细", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult editSave(@RequestBody JcLedgerDetailVo jcLedgerDetailVo) {
        JcLedgerDetail olderDetail = jcLedgerDetailService.selectJcLedgerDetailById(jcLedgerDetailVo.getId());
        if (olderDetail != null && !olderDetail.getCreateName().equals(getUsername())) {
            return error("修改失败,禁止修改他人数据！");
        }
        JcLedgerDetail jcLedgerDetail = jcLedgerDetailConvert.toDO(jcLedgerDetailVo);
        return toAjax(jcLedgerDetailService.updateJcLedgerDetail(jcLedgerDetail));
    }

    /**
     * 删除费用台帐明细
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("删除费用台帐明细")
    @PreAuthorize("@ss.hasPermi('api:ledger:remove')")
    @Log(title = "费用台帐明细", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(Long[] ids) {
        List<JcLedgerDetail> list = jcLedgerDetailService.listByIds(Arrays.asList(ids));
        if (list != null && list.size() > 0) {
            for (JcLedgerDetail olderDetail : list) {
                if (olderDetail != null && !olderDetail.getCreateName().equals(getUsername())) {
                    return error("删除失败,禁止删除他人数据！");
                }
            }
        }

        return toAjax(jcLedgerDetailService.deleteJcLedgerDetailByIds(ids));
    }
}
